/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.framework.implementation;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import com.zhangman.framework.Graphics;
import com.zhangman.framework.Image;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by zhangman on 2016/11/1 10:51.
 * Email: zhangman523@126.com
 */
public class AndroidGraphics implements Graphics {
  AssetManager mAssetManager;
  Bitmap mFrameBuffer;
  Canvas mCanvas;
  Paint mPaint;
  Rect srcRect = new Rect();
  Rect dstRect = new Rect();

  public AndroidGraphics(AssetManager assetManager, Bitmap frameBuffer) {
    this.mAssetManager = assetManager;
    this.mFrameBuffer = frameBuffer;
    this.mCanvas = new Canvas(frameBuffer);
    this.mPaint = new Paint();
  }

  @Override public Image newImage(String fileName, ImageFormat format) {
    Bitmap.Config config = null;
    if (format == ImageFormat.RGB565) {
      config = Bitmap.Config.RGB_565;
    } else if (format == ImageFormat.ARGB4444) {
      config = Bitmap.Config.ARGB_4444;
    } else {
      config = Bitmap.Config.ARGB_8888;
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = config;

    InputStream inputStream = null;
    Bitmap bitmap = null;
    try {
      inputStream = mAssetManager.open(fileName);
      bitmap = BitmapFactory.decodeStream(inputStream, null, options);
      if (bitmap == null) {
        throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
        }
      }
    }
    if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
      format = ImageFormat.RGB565;
    } else if (bitmap.getConfig() == Bitmap.Config.ARGB_4444) {
      format = ImageFormat.ARGB4444;
    } else {
      format = ImageFormat.ARGB8888;
    }
    return new AndroidImage(bitmap, format);
  }

  @Override public void clearScreen(int color) {
    mCanvas.drawRGB(color & 0xff0000 >> 16, (color & 0xff00) >> 8, (color & 0xff));
  }

  @Override public void drawLine(int x, int y, int x2, int y2, int color) {
    mPaint.setColor(color);
    mCanvas.drawLine(x, y, x2, y2, mPaint);
  }

  @Override public void drawRect(int x, int y, int width, int height, int color) {
    mPaint.setColor(color);
    mCanvas.drawRect(x, y, x + width - 1, y + height - 1, mPaint);
  }

  @Override public void drawImage(Image image, int x, int y) {
    mCanvas.drawBitmap(((AndroidImage) image).bitmap, srcRect, dstRect, null);
  }

  @Override public void drawImage(Image image, int x, int y, int srcX, int srcY, int srcWidth,
      int srcHeight) {
    srcRect.left = srcX;
    srcRect.top = srcY;
    srcRect.right = srcX + srcWidth;
    srcRect.bottom = srcY + srcHeight;

    dstRect.left = x;
    dstRect.top = y;
    dstRect.right = x + srcWidth;
    dstRect.bottom = y + srcHeight;
    mCanvas.drawBitmap(((AndroidImage) image).bitmap, srcRect, dstRect, null);
  }

  @Override
  public void drawScaledImage(Image image, int x, int y, int width, int height, int srcX, int srcY,
      int srcWidth, int srcHeight) {
    srcRect.left = srcX;
    srcRect.top = srcY;
    srcRect.right = srcX + srcWidth;
    srcRect.bottom = srcY + srcHeight;

    dstRect.left = x;
    dstRect.top = y;
    dstRect.right = x + width;
    dstRect.bottom = y + height;
    mCanvas.drawBitmap(((AndroidImage) image).bitmap, srcRect, dstRect, null);
  }

  @Override public void drawString(String text, int x, int y, Paint paint) {
    mCanvas.drawText(text, x, y, paint);
  }

  @Override public int getWidth() {
    return mFrameBuffer.getWidth();
  }

  @Override public int getHeight() {
    return mFrameBuffer.getHeight();
  }

  @Override public void drawARGB(int alpha, int red, int green, int blue) {
    mPaint.setStyle(Paint.Style.FILL);
    mCanvas.drawARGB(alpha, red, green, blue);
  }
}
